package jhta;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.swing.JOptionPane;

public class JavaDBConnection {
	private static String driver = "oracle.jdbc.driver.OracleDriver";
	static String url = "";
	static String serverIP = "";
	private static String db_user = "bookrent";
	private static String db_pwd = "bookrent";
	
	private static Connection conn;
	

	public static Connection getConn(){
		getServerIp();
		url = "jdbc:oracle:thin:@" + serverIP + ":1521:xe";
		
		if(conn == null ) connect();
		
		return conn;
	}
	
	
	public static Connection getConn(String ip){
		serverIP = ip;
		url = "jdbc:oracle:thin:@" + serverIP + ":1521:xe";
		saveConfiguration();
		connect();
		
		return conn;
	}
	
	public static void connect(){
		String sql = "";
		try{
			// 데이터베이스 연결 테스트
			// 1) 드라이버 로딩
			Class.forName(driver);
			System.out.println("드라이버가 로딩되었습니다.");

			// 시스템 유저로 접속
			conn = DriverManager.getConnection(url, "system", "1234");
			
			sql = "select count(*) as cnt from dba_users where username='BOOKRENT'";
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			
			if(rs.next()){
				int cnt = rs.getInt("cnt");
				if(cnt == 0){
					
					System.out.println("cnt:" + cnt);
					sql = "create user bookrent identified by bookrent";
					stmt.execute(sql);
					
					sql = "grant resource, connect to bookrent";
					stmt.execute(sql);
					System.out.println("bookrent 유저가 추가되었습니다.");
				}
				
				// connection 재 연결
				conn.close();
				conn = null;
				rs.close();
				stmt.close();
				
			}
			conn = DriverManager.getConnection(url, db_user, db_pwd);
			System.out.println("데이터베이스가 정상적으로 연결됨");
			
		}catch(Exception ex){
			JOptionPane.showMessageDialog(null, "데이터베이스가 없거나 데이터베이스 관리가 계정이 다릅니다.");
		}

	}

	private static void getServerIp(){
		try{
			File f = new File(Main.fileName);
			FileInputStream fis;
			BufferedReader br;
			if(f.exists()){
				 fis = new FileInputStream(Main.fileName);
				 br = new BufferedReader(new InputStreamReader(fis));
				 serverIP = br.readLine();
				 
				 br.close();
				 fis.close();
			}else{
				serverIP = JOptionPane.showInputDialog(null, "데이터베이스 서버가 설정되지 않았습니다\n서버 주소를 입력해 주세요");
				if(serverIP.equals("")) return;
				saveConfiguration();
			}
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
	}
	private static void saveConfiguration(){
		FileOutputStream fos=null;
		BufferedWriter bw = null;
		try{
			fos = new FileOutputStream(Main.fileName);
			bw = new BufferedWriter(new OutputStreamWriter(fos));
			bw.write(serverIP);
			bw.close();
			fos.close();
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}	



	
}
